import sys, stat
from os.path import exists, join
import os.path
from os import pathsep
import time
import datetime
import numpy as np
from astropy.table import Table,Column

def func_object_sort(rows_scheduled, current_utc_datetime):
    delta_datetime = []
    for m in rows_scheduled['insert_time']:
        insert_time_datetime = datetime.datetime.strptime(m, "%Y/%m/%dT%H:%M:%S")
        delta_datetime.append((current_utc_datetime - insert_time_datetime).total_seconds())

    delta_datetime = Column(data=delta_datetime,name=('delta_datetime'),dtype=('f'))
    if len(rows_scheduled['obj_id']) > 0:
        rows_scheduled.add_column(delta_datetime)
        data_new = rows_scheduled
        order_index = np.lexsort((data_new['objra'],data_new['priority']))
        order_index_reverse = order_index[::-1]
        data_order = data_new[order_index_reverse]

        gwac_index = np.where((data_order['priority'] >= 0) & (data_order['priority'] <= 19))[0]
        if len(gwac_index) > 0:
            data_gwac = data_order[gwac_index]
        oba_obm_index = np.where((data_order['priority'] >= 20) & (data_order['priority'] <= 29))[0]
        if len(oba_obm_index) > 0:
            data_oba_obm = data_order[oba_obm_index]
        rot_low_index = np.where((data_order['priority'] >= 30) & (data_order['priority'] <= 39))[0]
        if len(rot_low_index) > 0:
            data_rot_low = data_order[rot_low_index]
        goa_gom_index = np.where((data_order['priority'] >= 40) & (data_order['priority'] <= 59))[0]
        if len(goa_gom_index) > 0:
            data_goa_gom = data_order[goa_gom_index]
        toa_tom_index = np.where((data_order['priority'] >= 60) & (data_order['priority'] <= 79))[0]
        if len(toa_tom_index) > 0:
            data_toa_tom = data_order[toa_tom_index]
        rot_high_index = np.where((data_order['priority'] >= 80) & (data_order['priority'] <= 89))[0]
        if len(rot_high_index) > 0:
            data_rot_high = data_order[rot_high_index]
        cal_index = np.where((data_order['priority'] >= 90) & (data_order['priority'] <= 99))[0]
        if len(cal_index) > 0:
            data_cal = data_order[cal_index]
        man_index = np.where(data_order['priority'] >= 100)[0]
        if len(man_index) > 0:
            data_man = data_order[man_index]

        data_order_new = Table(names=('obj_id', 'tw_begin', 'tw_end','mode'),dtype=('a19','a19','a19','a19'))
        if len(man_index) > 0:
            for m in range(len(data_man)):
                data_order_new.add_row([data_man[m]['obj_id'],data_man[m]['tw_begin'],data_man[m]['tw_end'],data_man[m]['mode']])
        if len(cal_index) > 0:
            for m in range(len(data_cal)):
                data_order_new.add_row([data_cal[m]['obj_id'],data_cal[m]['tw_begin'],data_cal[m]['tw_end'],data_cal[m]['mode']])
        if len(rot_high_index) > 0:
            for m in range(len(data_rot_high)):
                data_order_new.add_row([data_rot_high[m]['obj_id'],data_rot_high[m]['tw_begin'],data_rot_high[m]['tw_end'],data_rot_high[m]['mode']])
        if len(toa_tom_index) > 0:
            toa_tom_order_index = np.lexsort((data_toa_tom['objrank'],data_toa_tom['priority'])) # ordering by ranking and priority
            toa_tom_order_index_reverse = toa_tom_order_index[::-1]
            data_toa_tom_order = data_toa_tom[toa_tom_order_index_reverse]
            for m in range(len(data_toa_tom_order)):
                data_order_new.add_row([data_toa_tom_order[m]['obj_id'],data_toa_tom_order[m]['tw_begin'],data_toa_tom_order[m]['tw_end'],data_toa_tom_order[m]['mode']])
        if len(goa_gom_index) > 0:
            goa_gom_order_index = np.lexsort((data_goa_gom['delta_datetime'],data_goa_gom['priority'])) # ordering by insert_time and priority
            goa_gom_order_index_reverse = goa_gom_order_index[::-1]
            data_goa_gom_order = data_goa_gom[goa_gom_order_index_reverse]
            for m in range(len(data_goa_gom_order)):
                data_order_new.add_row([data_goa_gom_order[m]['obj_id'],data_goa_gom_order[m]['tw_begin'],data_goa_gom_order[m]['tw_end'],data_goa_gom_order[m]['mode']])
        if len(rot_low_index) > 0:
            for m in range(len(data_rot_low)):
                data_order_new.add_row([data_rot_low[m]['obj_id'],data_rot_low[m]['tw_begin'],data_rot_low[m]['tw_end'],data_rot_low[m]['mode']])
        if len(oba_obm_index) > 0: # ording by ra
            oba_obm_order_index = np.lexsort((data_oba_obm['objra'],data_oba_obm['objrank'],data_oba_obm['priority']))
            oba_obm_order_index_reverse = oba_obm_order_index[::-1]
            data_oba_obm_order = data_oba_obm[oba_obm_order_index_reverse]
            for m in range(len(data_oba_obm_order)):
                data_order_new.add_row([data_oba_obm_order[m]['obj_id'],data_oba_obm_order[m]['tw_begin'],data_oba_obm_order[m]['tw_end'],data_oba_obm_order[m]['mode']])
        if len(gwac_index) > 0: # ording by ra
            gwac_order_index = np.lexsort((data_gwac['objra'],data_gwac['objrank']))
            gwac_order_index_reverse = gwac_order_index[::-1]
            data_gwac_order = data_gwac[gwac_order_index_reverse]
            for m in range(len(data_gwac_order)):
                data_order_new.add_row([data_gwac_order[m]['obj_id'],data_gwac_order[m]['tw_begin'],data_gwac_order[m]['tw_end'],data_gwac_order[m]['mode']])
    else:
        data_order_new = Table(names=('obj_id', 'tw_begin', 'tw_end','mode'),dtype=('a19','a19','a19','a19'))
    return data_order_new





